package com.aiks.single.singlemavenproject.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * CORS支持.
 *
 * @author <a href="mailto:yuanz@yxt.com">yuanz</a>
 * @date 2021-08-26 10:09
 * @since 1.0.0
 **/
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    private static final String[] VAL_ALLOW_HEADERS = new String[]{"Accept", "Content-Type", "Source", "Sourcetype", "Token", "Sign", "Cache-Control", "clientKey", "deviceId", "OrgId", "accessKey",
            "appKey", "nonce", "timestamp", "signature", "clientToken", "AppId", "App-Version", "RequestClientIp", "Power-by"};
    private static final String[] VAL_EXPOSE_HEADERS = new String[]{"Content-Type", "Date", "Location", "Cache-Control"};
    private static final String[] VAL_ALLOW_METHODS = new String[]{"GET", "PUT", "POST", "DELETE", "OPTIONS"};
    private static final boolean VAL_ALLOW_CREDENTIALS = true;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //		List<String> allowedOriginPatterns = new ArrayList<>();
        //		allowedOriginPatterns.add("*");
        registry.addMapping("/**")
            .allowedOrigins("*")
            .allowedHeaders(VAL_ALLOW_HEADERS)
            .allowedMethods(VAL_ALLOW_METHODS)
            .exposedHeaders(VAL_EXPOSE_HEADERS)
            .allowCredentials(VAL_ALLOW_CREDENTIALS).maxAge(3600L);
    }
}
